<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>打印测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
            padding: 0;
            background-color: #f4f4f9;
        }

        h1 {
            text-align: center;
            color: #333;
        }

        .container {
            max-width: 600px;
            margin: 0 auto;
            background: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }

        label {
            font-weight: bold;
            margin-top: 10px;
            display: block;
        }

        textarea {
            width: 100%;
            height: 150px;
            margin-bottom: 20px;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 4px;
            font-size: 14px;
        }

        input, select {
            width: 100%;
            padding: 10px;
            margin-bottom: 20px;
            border: 1px solid #ccc;
            border-radius: 4px;
        }

        button {
            display: block;
            width: 100%;
            background-color: #007BFF;
            color: white;
            padding: 10px 15px;
            font-size: 16px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }

        button:hover {
            background-color: #0056b3;
        }

        .output {
            margin-top: 20px;
            padding: 10px;
            background: #e9ecef;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
        }

        .footer {
            text-align: center;
            margin-top: 20px;
            color: #666;
        }
    </style>
</head>
<body>
    <h1>打印测试</h1>
    <div class="container">
        <form id="printForm">
            <label for="host">服务地址：</label>
            <input type="text" id="host" value="localhost" placeholder="请输入服务地址，例如：localhost">
            <label for="port">端口：</label>
            <input type="number" id="port" value="5000" placeholder="请输入端口，例如：5000">
            <label for="htmlContent">请输入要打印的内容（HTML 格式）：</label>
            <textarea id="htmlContent" placeholder="例如：<h1>测试打印</h1>"></textarea>
            <label for="printerName">打印机名称（可选）：</label>
            <input type="text" id="printerName" placeholder="例如：Microsoft Print to PDF">

            <button type="button" onclick="submitPrintRequest()">发送打印请求</button>
        </form>

        <div id="output" class="output" style="display: none;"></div>
    </div>
    <div class="footer">
    </div>

    <script>
        function submitPrintRequest() {
            const host = document.getElementById('host').value.trim() || 'localhost';
            const port = document.getElementById('port').value.trim() || '5000';
            const htmlContent = document.getElementById('htmlContent').value.trim();
            const printerName = document.getElementById('printerName').value.trim();
            const output = document.getElementById('output');

            if (!htmlContent) {
                alert("请输入 HTML 内容！");
                return;
            }

            const apiUrl = `http://${host}:${port}/print`;

            // 发送 POST 请求到打印服务
            fetch(apiUrl, {
                method: "POST",
                headers: {
                    "Content-Type": "application/json"
                },
                body: JSON.stringify({
                    html: htmlContent,
                    printer_name: printerName ? printerName : null
                })
            })
            .then(response => response.json())
            .then(data => {
                output.style.display = "block";
                if (data.status === "success") {
                    output.innerHTML = `<strong style="color: green;">打印成功：</strong> ${data.message}`;
                } else {
                    output.innerHTML = `<strong style="color: red;">打印失败：</strong> ${data.message}`;
                }
            })
            .catch(error => {
                output.style.display = "block";
                output.innerHTML = `<strong style="color: red;">错误：</strong> 无法连接到打印服务，请检查服务是否已启动。`;
            });
        }
    </script>
</body>
</html>
